package com.wry.wry_test.algorithm;

import java.util.regex.Pattern;

/**
 * @Author: wangrongyi
 * @Date: 2025/1/16 18:15
 * @Description:
 */
public class Day20 {
    // 正则表达式，用于匹配符合 UUID v4 格式的字符串
    private static final String UUID_V4_REGEX =
            "^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$";

    private static final Pattern UUID_V4_PATTERN = Pattern.compile(UUID_V4_REGEX, Pattern.CASE_INSENSITIVE);

    /**
     * 校验是否为有效的 UUID v4 格式
     *
     * @param uuid 要校验的字符串
     * @return 如果是有效的 UUID v4 格式，返回 true；否则返回 false
     */
    public static boolean isValidUUIDv4(String uuid) {
        if (uuid == null) {
            return false;
        }
        return UUID_V4_PATTERN.matcher(uuid).matches();
    }

    public static void main(String[] args) {
        // 测试用例
        String validUUID = "3f4b1c40-82e2-4bfa-a123-9d4b34e6f7d1";
        String invalidUUID = "3f4b1c40-82e2-5bfa-a123-9d4b34e6f7d1"; // 版本号不是 4

        System.out.println("Valid UUID: " + isValidUUIDv4(validUUID)); // true
        System.out.println("Invalid UUID: " + isValidUUIDv4(invalidUUID)); // false
    }
}
